package lxbAdmin.userSystem.controller;

import io.netty.util.internal.StringUtil;
import lxbAdmin.userSystem.common.ErrorConstants;
import lxbAdmin.userSystem.common.ResponseData;
import lxbAdmin.userSystem.dto.User;
import lxbAdmin.userSystem.entity.SystemUser;
import lxbAdmin.userSystem.server.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/sys/user")
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService;

    /**
     * 查询用户
     *
     * @param user
     * @return
     */
    @PostMapping("/select")
    public ResponseData getSystemUser(@RequestBody User user) {
        return ResponseData.success(systemUserService.getSystemUser(user));
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @PostMapping("/add")
    public ResponseData addSystemUser(@RequestBody User user) {
        try {
            List<SystemUser> allUser = systemUserService.selectAllUser(user);
            if (!allUser.isEmpty()) return ResponseData.fail(ErrorConstants.WARN_MSG_1);
            if (StringUtil.isNullOrEmpty(user.getPassword())
                    || StringUtil.isNullOrEmpty(user.getLoginName())) {
                return ResponseData.fail(ErrorConstants.WARN_MSG_2);
            }
            return ResponseData.saveSuccess(systemUserService.addSystemUser(user));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.fail(ErrorConstants.WARN_MSG_3);
        }
    }


    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @PostMapping("/edit")
    public ResponseData editSystemUser(@RequestBody User user) {
        try {
            List<SystemUser> allUser = systemUserService.selectAllUser(user);
            if (!allUser.isEmpty()) return ResponseData.fail(ErrorConstants.WARN_MSG_4);
            if (StringUtil.isNullOrEmpty(user.getPassword())
                    || StringUtil.isNullOrEmpty(user.getLoginName()))
                return ResponseData.fail(ErrorConstants.WARN_MSG_2);
            systemUserService.editSystemUser(user);
            return ResponseData.editSuccess(user.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.fail(ErrorConstants.WARN_MSG_5);
        }
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @GetMapping("/delete")
    public ResponseData delSystemUser(@RequestParam("userId") String userId) {
        try {
            systemUserService.delSystemUser(userId);
            return ResponseData.delSuccess(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.fail(ErrorConstants.WARN_MSG_6);
        }
    }

    /**
     * 查询角色列表
     *
     * @return
     */
    @GetMapping("/selectRole")
    public ResponseData selectRole() {
        return ResponseData.success(systemUserService.selectRole());
    }

    /**
     * 查询详情
     *
     * @param userId
     * @return
     */
    @GetMapping("/detail")
    public ResponseData selectByUserId(@RequestParam("userId") String userId) {
        return ResponseData.success(systemUserService.selectByUserId(userId));
    }

}
